package edu.gatech.cs4235.group3;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import edu.gatech.cs4235.group3.domain.Message;
import edu.gatech.cs4235.group3.exception.OneTimePadException;

public class Test {
	public static void main(String[] args) throws OneTimePadException {
		Cracker crack = new Cracker();
		int padsInUse = 16;
		String sortedTextNames[][] = new String[padsInUse][3500];
		for(int i=0;i<padsInUse;i++){
			String temp = "encrypt/"+i+"OTP";
			File directory = new File(temp);
			String filename[] = directory.list();
			sortedTextNames[i] = filename;
			
		}
		System.out.println("SortedTextNames Done");
		//So sortedTextNames should contain all  the filenames sorted by their OTP
		//Lets make an iterable of correctly formatted ciphertexts 3461 ct of each otp
		List<char[][]> allTexts = new ArrayList<char[][]>();
		
		
		for(int j=0;j<3461;j++){
			for(int i=0;i<padsInUse;i++){
				Message temp = new Message("encrypt/"+i+"OTP/"+sortedTextNames[i][j]);
				//temp.printMessage();
				allTexts.add(temp.getText());
				System.out.println(allTexts.size());
			}
		}
		System.out.println("AllTexts Filled");
		char[][][] key;
		key = crack.crackMany(allTexts);
		char[][][] acKey = new char[padsInUse][50][80];
		//Lets get the OTPs back into a key[][][]
		File directory = new File("OTPs-RR");
		String filename[] = directory.list();
		
		for(int i=0;i<padsInUse;i++){
			acKey[i] = new Message("OTPs-RR/"+filename[i]).getText();
			
		}
		
		int missedKeys=0;
		System.out.println("key["+key.length+"]["+key[0].length+"]["+key[0][0].length+"]");
		for(int i=0;i<padsInUse;i++){
			for(int j=0;j<50;j++){
				for(int k=0;k<80;k++){
					if(key.length != acKey.length){
						System.out.println("Bad Key Count");
						return;
					}
					if(key[i][j][k] 
					             != acKey[i][j][k]){
						missedKeys++;
					}
				}
			}
		}
		System.out.println(missedKeys);
		
		
		
		/*Cracker crack = new Cracker();
		Analyzer anny = new Analyzer();
		List<char[][]> allTexts = new ArrayList<char[][]>();
		
		File directory = new File("encrypt");
		String filename[] = directory.list();
		Message temp;
		for(int i = 0; i < filename.length;i++){
			temp = new Message("encrypt/"+filename[i]);
			allTexts.add(temp.getText());
		}
		Message OTP = new Message("oneTimePad.txt");
		
		System.out.println("textsSide"+allTexts.size());
		char[][] guessedOTP = crack.crackOne(allTexts);
		//System.out.println("Done Cracking");
		int missedKeys = 0;
		for(int i=0;i<50;i++){
			for(int j=0;j<80;j++){
				if(OTP.getText()[i][j] != guessedOTP[i][j]){
					System.out.println(guessedOTP[i][j]+" =/= "+OTP.getText()[i][j]);
					missedKeys++;
				}
				else{
					//System.out.println("     "+guessedOTP[i][j]+" ===== "+OTP.getText()[i][j]);
				}
				
			}
		}
		System.out.println("Wrong Keys: "+missedKeys);
		/*
		System.out.println("===============================================");
		guessedOTP = anny.analyzeSingle(allTexts);
		//System.out.println("Done Cracking");
		missedKeys = 0;
		for(int i=0;i<50;i++){
			for(int j=0;j<80;j++){
				if(OTP.getText()[i][j] != guessedOTP[i][j]){
					System.out.println(guessedOTP[i][j]+" =/= "+OTP.getText()[i][j]);
					missedKeys++;
				}
				else{
					//System.out.println("     "+guessedOTP[i][j]+" ===== "+OTP.getText()[i][j]);
				}
				
			}
		}
		System.out.println("Wrong Keys: "+missedKeys);*/
		
	}
}
